﻿// <copyright file="RoleController.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Web.Cms.Admin.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Mvc;

    using CleverCMS.Cms.Data.Entities;
    using CleverCMS.Cms.Services;

    [Authorize(Roles = "Admin")]
    public class RoleController : Controller
    {
        private readonly AccountService m_userService;

        public RoleController(AccountService userService)
        {
            this.m_userService = userService;
        }

        public ViewResult List()
        {
           return View();
        }

        [HttpPost]
        public JsonResult ListRoles(int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
        {
            try
            {
                int count = 0;
                var allPages = m_userService.LoadAllRoles(jtStartIndex, jtPageSize, jtSorting, out count).Select(x => new { RoleId = x.RoleId, RoleName = x.RoleName }).ToList();
                return Json(new { Result = "OK", Records = allPages, TotalRecordCount = count });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }
        
        public ViewResult Edit(int? id)
        {
            return id.HasValue ? View(m_userService.LoadRole(id.Value)) : 
                                 View();
        }

        [HttpPost]
        [Authorize]
        public ActionResult Edit(Role role)
        {
            //if (ModelState.IsValid)
            //{
                if (role != null)
                {
                    m_userService.UpdateRole(role);
                    var v = new { success = true, error = "", id = role.RoleId};
                    return Json(v);
                }
                else
                {
                    var v = new { success = false, error = "Unknown error. Role is null!" };
                    return Json(v);
                }
            //}
            //else
            //{
            //    var v = new { success = false, error = "Unknown error. ModelState is not valid!" };
            //    return Json(v);
            //} 
        }

        [HttpPost]
        [Authorize]
        public ActionResult Delete(int? roleId)
        {
            if (!ModelState.IsValid)
            {
                var v = new { Result = "ERROR", Message = "", success = false, error = "Unknown error" };
                return Json(v);
            }

            try
            {
                if (roleId != null)
                {
                    m_userService.DeleteRole(roleId.Value);
                    var v = new { Result = "OK", Message = "", success = true, error = "", Id = roleId };
                    return Json(v);
                }
                else
                {
                    var v = new { Result = "ERROR", Message = "", success = false, error = "Unknown error" };
                    return Json(v);
                }
            }
            catch (Exception exc)
            {
                var v = new { Result = "ERROR", Message = exc.Message, success = false, error = exc.Message };
                return Json(v);
            }
        }

    }
}